package com.zg.common.core.dao.autoflow.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.zg.common.core.exception.BusinessException;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

@Data
public class SettleWorkflowInstance {

    public static final Set<String> DEF_COLUMNS_XIAOXIE = new HashSet<>();

    {
        DEF_COLUMNS_XIAOXIE.add("contractNo");
        DEF_COLUMNS_XIAOXIE.add("transDate");
        DEF_COLUMNS_XIAOXIE.add("startStation");
        DEF_COLUMNS_XIAOXIE.add("endStation");
        DEF_COLUMNS_XIAOXIE.add("receCompany");
        DEF_COLUMNS_XIAOXIE.add("netWater");
        DEF_COLUMNS_XIAOXIE.add("hydm");
        DEF_COLUMNS_XIAOXIE.add("tKb");
        DEF_COLUMNS_XIAOXIE.add("tMz");
        DEF_COLUMNS_XIAOXIE.add("tJsbh");
        DEF_COLUMNS_XIAOXIE.add("tCs");
        DEF_COLUMNS_XIAOXIE.add("tJsl");
        DEF_COLUMNS_XIAOXIE.add("tCbj");
        DEF_COLUMNS_XIAOXIE.add("tYhj");
        DEF_COLUMNS_XIAOXIE.add("tJsj");
        DEF_COLUMNS_XIAOXIE.add("tBz");
        DEF_COLUMNS_XIAOXIE.add("tMctze");
        DEF_COLUMNS_XIAOXIE.add("tXse");
        DEF_COLUMNS_XIAOXIE.add("tSe");
        DEF_COLUMNS_XIAOXIE.add("tJshj");
        DEF_COLUMNS_XIAOXIE.add("tYsk");
        DEF_COLUMNS_XIAOXIE.add("tPz");
        DEF_COLUMNS_XIAOXIE.add("tJsrq");
        DEF_COLUMNS_XIAOXIE.add("tBzl");
        DEF_COLUMNS_XIAOXIE.add("tHtj");
        DEF_COLUMNS_XIAOXIE.add("tYhfd");
        DEF_COLUMNS_XIAOXIE.add("tJsdcbj");
        DEF_COLUMNS_XIAOXIE.add("tMkjsje");
        DEF_COLUMNS_XIAOXIE.add("tQscf");
        DEF_COLUMNS_XIAOXIE.add("tSjxj");
        DEF_COLUMNS_XIAOXIE.add("tYf");
        DEF_COLUMNS_XIAOXIE.add("tZj");
        DEF_COLUMNS_XIAOXIE.add("tPch");
        DEF_COLUMNS_XIAOXIE.add("tNbcj");
        DEF_COLUMNS_XIAOXIE.add("fuKuanDanWei");
        DEF_COLUMNS_XIAOXIE.add("fuKuanJinE");
        DEF_COLUMNS_XIAOXIE.add("faReLiang");
        DEF_COLUMNS_XIAOXIE.add("tLiu");
        DEF_COLUMNS_XIAOXIE.add("tJldh");
        DEF_COLUMNS_XIAOXIE.add("tCpmc");
        DEF_COLUMNS_XIAOXIE.add("tNw");
        DEF_COLUMNS_XIAOXIE.add("tPm");
        DEF_COLUMNS_XIAOXIE.add("tZcbh");
        DEF_COLUMNS_XIAOXIE.add("tJsdw");
        DEF_COLUMNS_XIAOXIE.add("tSkdw");
        DEF_COLUMNS_XIAOXIE.add("tCm");
        DEF_COLUMNS_XIAOXIE.add("tGk");
        DEF_COLUMNS_XIAOXIE.add("tXsdw");
        DEF_COLUMNS_XIAOXIE.add("tScgs");
        DEF_COLUMNS_XIAOXIE.add("tZdgs");
        DEF_COLUMNS_XIAOXIE.add("tXszt");
        DEF_COLUMNS_XIAOXIE.add("tGmdw");
        DEF_COLUMNS_XIAOXIE.add("premiumInc");
        DEF_COLUMNS_XIAOXIE.add("premiumServiceFee");
        DEF_COLUMNS_XIAOXIE.add("isSealAcc");
        DEF_COLUMNS_XIAOXIE.add("tLyrqJs");
        DEF_COLUMNS_XIAOXIE.add("tConfirmationId");
        DEF_COLUMNS_XIAOXIE.add("tWldwmc");
        DEF_COLUMNS_XIAOXIE.add("tHtzt");
        DEF_COLUMNS_XIAOXIE.add("tJzhzrq");
        DEF_COLUMNS_XIAOXIE.add("tWldwye");
        DEF_COLUMNS_XIAOXIE.add("tJbr");
        DEF_COLUMNS_XIAOXIE.add("tDh");
        DEF_COLUMNS_XIAOXIE.add("tKhxy");
        DEF_COLUMNS_XIAOXIE.add("tSfxfsc");
        DEF_COLUMNS_XIAOXIE.add("tSfxfzz");
        DEF_COLUMNS_XIAOXIE.add("tFj");
        DEF_COLUMNS_XIAOXIE.add("reason");
        DEF_COLUMNS_XIAOXIE.add("tMtzl");
    }


    /**
     * 主键ID
     */
    @TableId(value = "ID",type = IdType.ASSIGN_ID)
    private Long id;

    /**
     * 实例编号(19位,4位英文_14位数字)
     */
    private String instCode;

    /**
     * 流程编号（6位，2英文+4数字序列）
     */
    private String flowCode;

    /**
     * 流程名称
     */
    private String flowName;

    /**
     * 流程实例状态（1进行中 2已结束 3已作废 4已删除）
     */
    private String flowInstState;

    /**
     * 流程实例当前环节
     */
    private String flowInstCurrStep;

    /**
     * 当前环节实例code
     */
    private String flowInstCurrStepInst;


    /**
     * 流程实例当前环节名称
     */
    private String flowInstCurrStepName;

    /**
     * 流程实例创建人
     */
    private String flowInstCreator;

    /**
     * 当前环节处理人
     */
    private String currStepOperator;

    /**
     * 创建时间
     */
    private Date createDate;

    /**
     * 完成时间
     */
    private Date successTime;

    /**
     * 合同编号
     */
    private String contractNo;

    /**
     * 流程编号
     */
    private String instanceNo;

    /**
     * 创建流程组织id
     */
    private String createOrgId;

    /**
     * 下一个流程的id
     */
    private String nextInstCode;

    /**
     * 发运日期
     */
    private String transDate;

    /**
     * 发站
     */
    private String startStation;

    /**
     * 到站
     */
    private String endStation;

    /**
     * 收货单位
     */
    private String receCompany;

    /**
     * 折水净量
     */
    private String netWater;

    /**
     * 客户会员代码
     */
    private String hydm;

    /**
     * 矿别
     */
    private String tKb;

    /**
     * 煤种
     */
    private String tMz;

    /**
     * 结算编号
     */
    private String tJsbh;

    /**
     * 车数
     */
    private Long tCs;

    /**
     * 结算量
     */
    private BigDecimal tJsl;

    /**
     * 车板价
     */
    private BigDecimal tCbj;

    /**
     * 优惠额
     */
    private BigDecimal tYhj;

    /**
     * 结算价
     */
    private BigDecimal tJsj;

    /**
     * 备注（调整说明）
     */
    private String tBz;

    /**
     * 煤差调整额
     */
    private BigDecimal tMctze;

    /**
     * 销售额
     */
    private BigDecimal tXse;

    /**
     * 税额
     */
    private BigDecimal tSe;

    /**
     * 价税合计
     */
    private BigDecimal tJshj;

    /**
     * 应收款
     */
    private BigDecimal tYsk;

    /**
     * 品种
     */
    private String tPz;

    /**
     * 结算日期
     */
    private String tJsrq;

    /**
     * 标重
     */
    private BigDecimal tBzl;

    /**
     * 合同价
     */
    private BigDecimal tHtj;

    /**
     * 优惠幅度
     */
    private BigDecimal tYhfd;

    /**
     * 结算单的车板价
     */
    private BigDecimal tJsdcbj;

    /**
     * 煤款结算金额
     */
    private BigDecimal tMkjsje;

    /**
     * 取送车费
     */
    private BigDecimal tQscf;

    /**
     * 税金小计
     */
    private BigDecimal tSjxj;

    /**
     * 运费
     */
    private BigDecimal tYf;

    /**
     * 总计
     */
    private BigDecimal tZj;

    /**
     * 批次号
     */
    private String tPch;

    /**
     * 内部差价
     */
    private BigDecimal tNbcj;

    /**
     * 付款单位
     */
    private String fuKuanDanWei;

    /**
     * 付款金额
     */
    private String fuKuanJinE;

    /**
     * 发热量
     */
    private String faReLiang;

    /**
     * 硫
     */
    private String tLiu;

    /**
     * 计量单号
     */
    private String tJldh;

    /**
     * 产品名称
     */
    private String tCpmc;

    /**
     * 过衡重量
     */
    private String tNw;

    /**
     * 作废原因
     */
    private String reason;

    /**
     * 小类
     */
    private String tPm;

    /**
     * 装船编号
     */
    private String tZcbh;

    private String tJsdw;

    private String tSkdw;
    /**
     * 船名
     */
    private String tCm;

    private String tGk;


    private String tXsdw;

    /**
     * 市场公司
     */
    private String tScgs;

    /**
     * 驻地公司
     */
    private String tZdgs;

    /**
     * 销售主体
     */
    private String tXszt;

    /**
     * 供煤单位
     */
    private String tGmdw;

    /**
     * 是否封账
     */
    private String isSealAcc;

    /**
     * 删除标识
     */
    private String deleteState = "0";

    /**
     * 溢价增收
     */
    public BigDecimal premiumInc;

    /**
     * 溢价服务费
     */
    public BigDecimal premiumServiceFee;

    /**
     * 拉运结束日期
     */
    public String tLyrqJs;

    /**
     * 港口线上发运单主键ID
     */
    public String tConfirmationId;

    /**
     * 往来单位名称
     */
    public String tWldwmc;
    /**
     * 合同主体
     */
    public String tHtzt;
    /**
     * 截止函证日期
     */
    public String tJzhzrq;
    /**
     * 往来单位余额
     */
    public String tWldwye;
    /**
     * 经办人
     */
    public String tJbr;
    /**
     * 电话
     */
    public String tDh;
    /**
     * 客户需要
     */
    public String tKhxy;
    /**
     * 是否相符（首次）
     */
    public String tSfxfsc;
    /**
     * 是否相符（最终）
     */
    public String tSfxfzz;
    /**
     * 附件
     */
    public String tFj;

    /**
     * 煤炭种类
     */
    public String tMtzl;


    /**
     * 设置当前环节
     *
     * @param step
     */
    public void setCurStep(SettleWorkflowStepInstance step) {
        this.flowInstCurrStep = step.getStepCode();

        this.flowInstCurrStepInst = step.getStepInstCode();

        this.flowInstCurrStepName = step.getStepName();
    }

    public void setDefColumns(SettleWorkflowInstance byCode) {
        this.contractNo = byCode.contractNo;
        this.transDate = byCode.transDate;
        this.startStation = byCode.startStation;
        this.endStation = byCode.endStation;
        this.receCompany = byCode.receCompany;
        this.netWater = byCode.netWater;
        this.hydm = byCode.hydm;
        this.tKb = byCode.tKb;
        this.tMz = byCode.tMz;
        this.tJsbh = byCode.tJsbh;
        this.tCs = byCode.tCs;
        this.tJsl = byCode.tJsl;
        this.tCbj = byCode.tCbj;
        this.tYhj = byCode.tYhj;
        this.tJsj = byCode.tJsj;
        this.tBz = byCode.tBz;
        this.tMctze = byCode.tMctze;
        this.tXse = byCode.tXse;
        this.tSe = byCode.tSe;
        this.tJshj = byCode.tJshj;
        this.tYsk = byCode.tYsk;
        this.tPz = byCode.tPz;
        this.tJsrq = byCode.tJsrq;
        this.tBzl = byCode.tBzl;
        this.tHtj = byCode.tHtj;
        this.tYhfd = byCode.tYhfd;
        this.tJsdcbj = byCode.tJsdcbj;
        this.tMkjsje = byCode.tMkjsje;
        this.tQscf = byCode.tQscf;
        this.tSjxj = byCode.tSjxj;
        this.tYf = byCode.tYf;
        this.tZj = byCode.tZj;
        this.tPch = byCode.tPch;
        this.tNbcj = byCode.tNbcj;
        this.fuKuanDanWei = byCode.fuKuanDanWei;
        this.fuKuanJinE = byCode.fuKuanJinE;
        this.faReLiang = byCode.faReLiang;
        this.tLiu = byCode.tLiu;
        this.tJldh = byCode.tJldh;
        this.tCpmc = byCode.tCpmc;
        this.tNw = byCode.tNw;
        this.tPm = byCode.tPm;
        this.tZcbh = byCode.tZcbh;
        this.tJsdw = byCode.tJsdw;
        this.tSkdw = byCode.tSkdw;
        this.tCm = byCode.tCm;
        this.tGk = byCode.tGk;
        this.tScgs = byCode.tScgs;
        this.tZdgs= byCode.tZdgs;
        this.tXszt= byCode.tXszt;
        this.tGmdw= byCode.tGmdw;
        this.premiumInc= byCode.premiumInc;
        this.premiumServiceFee= byCode.premiumServiceFee;
        this.isSealAcc= byCode.isSealAcc;
        this.tLyrqJs = byCode.tLyrqJs;
        this.tConfirmationId = byCode.tConfirmationId;
        this.tWldwmc = byCode.tWldwmc;
        this.tHtzt = byCode.tHtzt;
        this.tJzhzrq = byCode.tJzhzrq;
        this.tWldwye = byCode.tWldwye;
        this.tJbr = byCode.tJbr;
        this.tDh = byCode.tDh;
        this.tKhxy = byCode.tKhxy;
        this.tSfxfsc = byCode.tSfxfsc;
        this.tSfxfzz = byCode.tSfxfzz;
        this.tFj = byCode.tFj;
        this.tMtzl = byCode.tMtzl;
    }

    public void setDefValue(String key, Object str) {

        String value =null;
        if(str!=null)
        {
            String s = str.toString();
            if(StringUtils.isNotBlank(s))
            {
                value = s;
            }
        }

        if (!DEF_COLUMNS_XIAOXIE.contains(key)) {
            BusinessException.happen("固有字段错误" + key + value);
        }
        try {
            //所有的字段
            Field declaredField = SettleWorkflowInstance.class.getDeclaredField(key);
            declaredField.setAccessible(true);
            if (declaredField.getType() == String.class) {
                if (value != null) {
                    declaredField.set(this, value.toString());
                }
                else{
                    declaredField.set(this,"");
                }

            } else if (declaredField.getType() == BigDecimal.class) {
                if (value != null) {
                    String s = value.toString();
                    declaredField.set(this,  new BigDecimal(s));
                }
                else{
                    declaredField.set(this,new BigDecimal(""));
                }

            }
            else if (declaredField.getType() == Long.class) {
                if (value != null) {
                    String s = value.toString();
                    declaredField.set(this,  Long.parseLong(s));
                }
                else{
                    declaredField.set(this,Long.parseLong(""));
                }

            }else {
                BusinessException.happen("固有字段错误" + key + value);
            }
        } catch (Exception e) {
            e.printStackTrace();
            BusinessException.happen("固有字段错误" + key + value);
        }

    }


    //根据固有字段获取值
    public String getDefValue(String def) {
        if (StringUtils.isBlank(def)) {
            return null;
        }
        //所有的字段
        Field[] declaredFields = SettleWorkflowInstance.class.getDeclaredFields();
        Map<String, Field> declaredFieldsmap = new HashMap<>();
        for (Field field : declaredFields) {
            TableField annotation = field.getAnnotation(TableField.class);
            if (annotation != null) {
                String value = annotation.value();
                declaredFieldsmap.put(value, field);
            }
        }

        Field field = declaredFieldsmap.get(def);
        if (field == null) {
            return null;
        }

        Object o = null;
        try {
            o = field.get(this);
            if (o == null) {
                return null;
            }
            return o.toString();
        } catch (Exception e) {
            return "";
        }
    }

    //获取所有固有字段
    public Map<String, Object> getAllDefMap() {
        Field[] declaredFields = SettleWorkflowInstance.class.getDeclaredFields();
        Map<String, Object> map = new HashMap<>();
        for (Field declaredField:declaredFields
             ) {
            declaredField.setAccessible(true);

            TableField annotation = declaredField.getAnnotation(TableField.class);
            if(annotation!=null)
            {
                try {
                    map.put(annotation.value(),declaredField.get(this));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }


    /**
     * 获取结算月份
     * @return
     */
    public String getJSY()
    {
        if(tJsrq == null || "".equals(tJsrq))
        return "";
        return  tJsrq.substring(0,7).replace("-","");
    }
}
